/*
 * LegacyUI.java
 *
 * Created on January 2, 2006, 12:42 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package net.jxta.myjxta.util.preferences;

import info.clearthought.layout.TableLayout;
import net.jxta.myjxta.util.Resources;
import org.jdesktop.swingx.JXPanel;
import org.jdesktop.swingx.JXTitledPanel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Hashtable;
import java.util.ResourceBundle;

/**
 *
 * @author polo
 */
public final class LegacyUI extends JXTitledPanel{
    //private static final Logger LOG = Logger.getLogger(LegacyUI.class);
    private static final ResourceBundle STRINGS = Resources.getStrings();
    //private static final String DEFAULT_ICON_NAME = "TreeTable.PeerPresent";
    private LegacyPreferenceSet prefs = null;
    private Hashtable<String, ButtonModel> preferenceTable = null;
    private static final Dimension DEFAULT_DIMENSION = new Dimension(300,200);
    
    /**
     * Creates a new instance of LegacyUI
     */
    public LegacyUI(LegacyPreferenceSet prefs) {
        super();
        this.prefs = prefs;
        //this.setTitle("xuanxiang");
        /*this.setTitle(STRINGS.getString(prefs.getAttribute(PreferenceSet.ATTRIBUTE_LABEL)));*/
        preferenceTable = new Hashtable<String, ButtonModel>();
        //setSize(300,300);
        //setVisible(true);
        buildUI();
    }
    
    
    
    private void buildUI() {
        
        
        Dimension d = MyPreferenceManager.stringToDimension(prefs.getAttribute("dimension"));
        if(d == null) {
            d= DEFAULT_DIMENSION;
        }
        double[][] sizes = {{d.getWidth() },
        { TableLayout.PREFERRED,TableLayout.PREFERRED,TableLayout.PREFERRED,
                  TableLayout.PREFERRED,TableLayout.PREFERRED,TableLayout.PREFERRED,
                  TableLayout.PREFERRED,TableLayout.PREFERRED,TableLayout.PREFERRED,
                  TableLayout.PREFERRED,TableLayout.PREFERRED,TableLayout.PREFERRED}};
                  
                  TableLayout tl = new TableLayout(sizes);
                  
                  JXPanel panel = new JXPanel(tl);
                  
                  panel.setDrawGradient(true);
                  int row = 0;
                  
//                  buildCheckBox(panel, LegacyPreferenceSet.PREFERENCE_CHIME , "0,"+String.valueOf(row++));
//                  buildCheckBox(panel, LegacyPreferenceSet.PREFERENCE_FREETTS , "0," +String.valueOf(row++));
                  buildCheckBox(panel, LegacyPreferenceSet.PREFERENCE_GRAPH , "0," +String.valueOf(row++));
//                  buildCheckBox(panel, LegacyPreferenceSet.PREFERENCE_SHELL , "0,"+String.valueOf(row++));
//                  buildCheckBox(panel, LegacyPreferenceSet.PREFERENCE_ELIZA , "0," +String.valueOf(row++));
//                  buildCheckBox(panel, LegacyPreferenceSet.PREFERENCE_PINGFILTER , "0," +String.valueOf(row++));
//                  buildCheckBox(panel, LegacyPreferenceSet.PREFERENCE_POUNCELISTENER , "0," +String.valueOf(row++));
                  buildCheckBox(panel, LegacyPreferenceSet.PREFERENCE_PROXY_HTTP , "0," +String.valueOf(row++));
                  buildCheckBox(panel, LegacyPreferenceSet.PREFERENCE_RECONFIGURE , "0," +String.valueOf(row++));
                  
                  this.add(panel);
                  
                  
                  JButton b = new JButton();
                  
                  Font font = b.getFont();
                  float size = font.getSize();
                  Font newFont = font.deriveFont(size-50);
                  
                  setTitleFont(newFont);
    }
    
    private void buildCheckBox(JXPanel container, String preferenceName, String constraints) {
        if(prefs.preferenceExists(preferenceName)) {
            String label = STRINGS.getString(prefs.getPreferenceAttribute(preferenceName,MyPreferenceManager.ATTRIBUTE_LABEL));
            JCheckBox cb = new JCheckBox(label);
            cb.setActionCommand(preferenceName);
            cb.setToolTipText(prefs.getPreferenceAttribute(preferenceName,MyPreferenceManager.ATTRIBUTE_TOOLTIP));
            cb.setEnabled(Boolean.valueOf(prefs.getPreferenceAttribute(preferenceName,MyPreferenceManager.ATTRIBUTE_ENABLE)));
            cb.setVisible(Boolean.valueOf(prefs.getPreferenceAttribute(preferenceName,MyPreferenceManager.ATTRIBUTE_VISIBLE)));
            cb.setSelected(Boolean.valueOf(prefs.getPreference(preferenceName)));
            cb.addItemListener(new ItemListener() {
                public void itemStateChanged(ItemEvent ie) {
                    booleanPreferenceChanged(((JCheckBox)ie.getSource()).getActionCommand(), ie.getStateChange()==ItemEvent.SELECTED);
                    
                }
            });
            
            
            
            preferenceTable.put(preferenceName, cb.getModel());
            
            container.add(cb,constraints);
            
        }
    }
    
    private void booleanPreferenceChanged(String command, boolean selected) {
        prefs.booleanPreferenceChanged(command, selected);
    }
    
    
}
